#include "mainframe.h"
#include "ui_mainframe.h"
#include <QDebug>
#include<QFileDialog>
#include<QMessageBox>
#include"sourcemanage.h"
#include"widgetmanage.h"
#include "workflowdialog.h"
#include "ethernettestdialog.h"
#include "algorithm.h"

MainFrame::MainFrame(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::MainFrame)
{
    ui->setupUi(this);
    this->setWindowState(Qt::WindowMaximized);

    //pExpFrame=new ExpFrame(this,pExperiment);
    //pExpFrame->hide();
    pTuneManualFrame=new TuneManualFrame(this);
    pTuneManualFrame->hide();
    pTuneAutoFrame=new TuneAutoFrame(this);
    pTuneAutoFrame->hide();
    pCrossCalFrame=new CrossCalFrame(this);
    pCrossCalFrame->hide();
    pMassCalFrame=new MassCalFrame(this);
    pMassCalFrame->hide();
    pHardwareFrame=new HardwareFrame(this);
    pHardwareFrame->hide();
    pSequenceRunFrame=new SequenceRunFrame(this);
    //pSpectralLineFrame=new SpectralLineFrame(this,3);
    //pSpectralLineFrame->hide();

    pMassCalReportFrame=new MassCalReportFrame(this);
    pMassCalReportFrame->hide();
    pCrossCalReportFrame=new CrossCalReportFrame(this);
    pCrossCalReportFrame->hide();
    pHexaPoleCalFrame=new HexaPoleCalFrame(this);
    pHexaPoleCalFrame->hide();
    pQuantitativeSemiFrame=new QuantitativeSemiFrame(this);
    pQuantitativeSemiFrame->hide();
    pTuneAutoReportFrame=new TuneAutoReportFrame(this);
    pTuneAutoReportFrame->hide();

    ui->horizontalLayout_Main->insertWidget(0,pSequenceRunFrame);

    QMenu *pCreatExp=new QMenu(this);
    QAction *pCreateBlank=new QAction(tr("新建空白方法"),this);
    pCreatExp->addAction(pCreateBlank);
    connect(pCreateBlank,SIGNAL(triggered()),this,SLOT(OnCreateBlankExp()));
    QAction *pCreateExist=new QAction(tr("导入已有方法"),this);
    pCreatExp->addAction(pCreateExist);
    connect(pCreateExist,SIGNAL(triggered()),this,SLOT(OnCreateExistExp()));
    ui->toolButton_NewExp->setMenu(pCreatExp);

    Algorithm::InitRand();
    WM->AddWidget("SequenceRunFrame",pSequenceRunFrame);
    WM->AddWidget("MainFrame",this);
}

MainFrame::~MainFrame()
{
    delete ui;
}

//常用：实验
void MainFrame::on_toolButton_NewExp_clicked()//1 2
{
    QString filePath=QFileDialog::getOpenFileName(this, tr("Open File"),FM->GetDataBaseDir(),
                                                  tr("Bin files (*%1)").arg(FM->GetDataBaseSuffix()));
    if(filePath=="") return;

    Experiment *pExperiment=new Experiment;
    FM->LoadFromFile(filePath,pExperiment->GetAnalyteDataBase());//实验设置元素库
    ExpFrame *pWidget=new ExpFrame(this,pExperiment);
    QFileInfo fileInfo(pExperiment->GetFilePath());
    ui->tabWidgetTask->addTab(pWidget,QString("实验[%1]").arg(fileInfo.fileName()));
    ui->tabWidgetTask->setCurrentWidget(pWidget);

    connect(pWidget,SIGNAL(SIG_ExpSave(QString)),this,SLOT(UpdateExpName(QString)));
}

void MainFrame::on_toolButton_OpenExp_clicked()
{
   QString filePath=QFileDialog::getOpenFileName(this, tr("Open File"),FM->GetExpsDir(),
                                                 tr("Bin files (*%1)").arg(FM->GetExpsSuffix()));
   if(filePath=="") return;

   QFileInfo fileInfo(filePath);
   QString name=QString("实验[%1]").arg(fileInfo.fileName());
   if(IsOpened(name)==true) return;//

   Experiment *pExperiment=new Experiment;
   FM->LoadFromFile(filePath,pExperiment);
   ExpFrame *pWidget=new ExpFrame(this,pExperiment);
   pExperiment->SetFilePath(filePath);

   ui->tabWidgetTask->addTab(pWidget,name);
   ui->tabWidgetTask->setCurrentWidget(pWidget);
   qDebug()<<"open exp :"<<filePath;

   connect(pWidget,SIGNAL(SIG_ExpSave(QString)),this,SLOT(UpdateExpName(QString)));
}
void MainFrame::on_toolButton_SaveExp_clicked()
{
    QWidget *pWidget=ui->tabWidgetTask->currentWidget();
    if(pWidget==NULL) return;
    QString className=pWidget->metaObject()->className();
    if(className!="ExpFrame") return;

    ExpFrame *pExpFrame= static_cast<ExpFrame *>(pWidget);
    pExpFrame->SaveData();
}
void MainFrame::on_toolButton_CloseExp_clicked()
{
    QWidget *pWidget=ui->tabWidgetTask->currentWidget();
    if(pWidget==NULL) return;
    QString className=pWidget->metaObject()->className();
    if(className!="ExpFrame") return;

    on_tabWidgetTask_tabCloseRequested(ui->tabWidgetTask->indexOf(pWidget));
}

//常用：仪器调谐
void MainFrame::on_toolButton_ManualTune_clicked()
{
    QWidget *pWidget=pTuneManualFrame;
    ui->tabWidgetTask ->addTab(pWidget,tr("手动调谐"));
    ui->tabWidgetTask->setCurrentWidget(pWidget);
}
void MainFrame::on_toolButton_AutoTune_clicked()//open 1234
{
    QWidget *pWidget=new TuneAutoFrame(this);
    ui->tabWidgetTask ->addTab(pWidget,tr("自动调谐[新建]"));
    ui->tabWidgetTask->setCurrentWidget(pWidget);
}

//常用：仪器校正
void MainFrame::on_toolButton_AutoMassCal_clicked()
{
    QWidget *pWidget=pMassCalFrame;
    ui->tabWidgetTask ->addTab(pWidget,tr("自动质量校正"));
    ui->tabWidgetTask->setCurrentWidget(pWidget);
}

void MainFrame::on_toolButton_AutoTuneReport_clicked()
{
    QWidget *pWidget=pTuneAutoReportFrame;
    ui->tabWidgetTask ->addTab(pWidget,tr("调谐报告[文件名]"));
    ui->tabWidgetTask->setCurrentWidget(pWidget);
}

void MainFrame::on_toolButton_AutoCrossCal_clicked()
{
    QWidget *pWidget=pCrossCalFrame;
    ui->tabWidgetTask ->addTab(pWidget,tr("交叉校正"));
    ui->tabWidgetTask->setCurrentWidget(pWidget);
}

void MainFrame::on_toolButton_HalfMassCal_clicked()
{
    QWidget *pWidget=pQuantitativeSemiFrame;
    ui->tabWidgetTask ->addTab(pWidget,tr("半定量校正"));
    ui->tabWidgetTask->setCurrentWidget(pWidget);
}

void MainFrame::on_toolButton_RfCal_clicked()
{
    QWidget *pWidget=pHexaPoleCalFrame;
    ui->tabWidgetTask ->addTab(pWidget,tr("六级杆校正"));
    ui->tabWidgetTask->setCurrentWidget(pWidget);
}

void MainFrame::on_toolButton_MassCalReport_clicked()
{
    QWidget *pWidget=pMassCalReportFrame;
    ui->tabWidgetTask ->addTab(pWidget,tr("质量校正报告"));
    ui->tabWidgetTask->setCurrentWidget(pWidget);
}

void MainFrame::on_toolButton_CrossCalReport_clicked()
{
    QWidget *pWidget=pCrossCalReportFrame;
    ui->tabWidgetTask ->addTab(pWidget,tr("交叉校正报告"));
    ui->tabWidgetTask->setCurrentWidget(pWidget);
}

void MainFrame::on_toolButton_InstrumentState_clicked()
{
    QWidget *pWidget=pHardwareFrame;
    ui->tabWidgetTask ->addTab(pWidget,tr("硬件"));
    ui->tabWidgetTask->setCurrentWidget(pWidget);
}

//管理：谱线
void MainFrame::on_toolButton_NewSpectralLine_clicked()
{
    AnalyteDataBase *pAnalyteDataBase=new AnalyteDataBase;
    SpectralLineFrame *pWidget=new SpectralLineFrame(this,pAnalyteDataBase,3);
    ui->tabWidgetTask ->addTab(pWidget,tr("谱线库[新建]"));
    ui->tabWidgetTask->setCurrentWidget(pWidget);
}
void MainFrame::on_toolButton_OpenSpectralLine_clicked()
{
    QString filePath=QFileDialog::getOpenFileName(this, tr("Open File"),FM->GetDataBaseDir(),
                                                  tr("Bin files (*%1)").arg(FM->GetDataBaseSuffix()));
    if(filePath=="") return;

    AnalyteDataBase *pAnalyteDataBase=new AnalyteDataBase;
    FM->LoadFromFile(filePath,pAnalyteDataBase);
    SpectralLineFrame *pWidget=new SpectralLineFrame(this,pAnalyteDataBase,0);
    QFileInfo fileInfo(filePath);
    pWidget->SetFileName(fileInfo.fileName());

    ui->tabWidgetTask->addTab(pWidget,QString("谱线库[%1]").arg(fileInfo.fileName()));
    ui->tabWidgetTask->setCurrentWidget(pWidget);
    qDebug()<<"open SpectralLine :"<<fileInfo.fileName();
}

//其他
void MainFrame::on_tabWidgetTask_tabCloseRequested(int index)
{

    QWidget *pWidget=ui->tabWidgetTask->widget(index);
    QString name=pWidget->objectName();

    qDebug()<<pWidget<<name;
    if(name=="TuneManualFrame")
    {
       ui->tabWidgetTask->removeTab(index);
    }
    else if(name=="MassCalFrame")
    {
       ui->tabWidgetTask->removeTab(index);
    }
    else if(name=="HardwareFrame")
    {
       ui->tabWidgetTask->removeTab(index);
    }
    else if(name=="TuneAutoFrame")
    {
       //save...
       ui->tabWidgetTask->removeTab(index);
       pWidget->deleteLater();
    }
    else if(name=="ExpFrame")
    {
        //save...
        ExpFrame *pExpFrame= static_cast<ExpFrame *>(pWidget);

        QMessageBox::StandardButton button=QMessageBox::question(this,tr("询问"),tr("是否保存对实验的修改？"));
        if(button==QMessageBox::Yes)
        {
            if(pExpFrame->SaveData()!=1)
            {

            }
        }
        else if(button==QMessageBox::No)
        {

        }
        else if(button==QMessageBox::Cancel||QMessageBox::Close)
        {

        }

        ui->tabWidgetTask->removeTab(index);
        pWidget->deleteLater();
    }
    else if(name=="SpectralLineFrame")
    {
        SpectralLineFrame *pSpectralLineFrame= static_cast<SpectralLineFrame *>(pWidget);

        QMessageBox::StandardButton button=QMessageBox::question(this,tr("询问"),tr("是否保存对谱线库的修改？"));
        if(button==QMessageBox::Yes)
        {
            if(pSpectralLineFrame->SaveData()!=1)
            {

            }
        }
        else if(button==QMessageBox::No)
        {

        }
        else if(button==QMessageBox::Cancel||QMessageBox::Close)
        {

        }

        ui->tabWidgetTask->removeTab(index);
        pWidget->deleteLater();
    }
    else
    {
        ui->tabWidgetTask->removeTab(index);
        //pWidget->deleteLater();
    }
}

//
QString MainFrame::GetCurrentWidget()
{
    QString name="";
    QWidget *pWidget=ui->tabWidgetTask->currentWidget();
    if(pWidget==NULL) return name;
    name=pWidget->metaObject()->className();
    return name;
}

int MainFrame::CloseWidget(QString name)
{
    if(name=="TuneAutoFrame")
    {

    }
    else if(name=="ExpFrame")
    {

    }
}

void MainFrame::AddTab(ExpFrame *pWidget,QString name)
{
    ui->tabWidgetTask->addTab(pWidget,name);
    ui->tabWidgetTask->setCurrentWidget(pWidget);
}
bool MainFrame::IsOpened(QString name)
{
    int count=ui->tabWidgetTask->count();
    qDebug()<<"count"<<count;
    for(int i=0;i<count;i++)
    {
        QString text=ui->tabWidgetTask->tabText(i);
        if(text==name)
        {
            ui->tabWidgetTask->setCurrentIndex(i);
            return true;
        }
    }

    return false;
}
//action
void MainFrame::OnCreateBlankExp()
{
    QString filePath=QFileDialog::getOpenFileName(this, tr("Open File"),FM->GetDataBaseDir(),
                                                  tr("Bin files (*%1)").arg(FM->GetDataBaseSuffix()));
    if(filePath=="") return;

    Experiment *pExperiment=new Experiment;
    FM->LoadFromFile(filePath,pExperiment->GetAnalyteDataBase());//实验设置元素库
    ExpFrame *pWidget=new ExpFrame(this,pExperiment);
    QFileInfo fileInfo(pExperiment->GetFilePath());
    ui->tabWidgetTask->addTab(pWidget,QString("实验[%1]").arg(fileInfo.fileName()));
    ui->tabWidgetTask->setCurrentWidget(pWidget);
}
void MainFrame::OnCreateExistExp()//是否清楚一些结果
{
    QString filePath=QFileDialog::getOpenFileName(this, tr("Open File"),FM->GetExpsDir(),
                                                  tr("Bin files (*%1)").arg(FM->GetExpsSuffix()));
    if(filePath=="") return;

    Experiment *pExperiment=new Experiment;
    FM->LoadFromFile(filePath,pExperiment);
    pExperiment->SetFilePath("新建");
    pExperiment->ClearResult();//清除结果

    ExpFrame *pWidget=new ExpFrame(this,pExperiment);
    QFileInfo fileInfo(pExperiment->GetFilePath());
    //QFileInfo fileInfo(filePath);
    ui->tabWidgetTask->addTab(pWidget,QString("实验[%1]").arg(fileInfo.fileName()));
    ui->tabWidgetTask->setCurrentWidget(pWidget);
}

//action
void MainFrame::UpdateExpName(QString name)
{
    QWidget *pWidget=ui->tabWidgetTask->currentWidget();
    if(pWidget==NULL) return;
    QString className=pWidget->metaObject()->className();
    if(className!="ExpFrame") return;

    ui->tabWidgetTask->setTabText(ui->tabWidgetTask->indexOf(pWidget),QString("实验[%1]").arg(name));
}


void MainFrame::on_toolButton_WorkFlow_clicked()
{
    WorkFlowDialog workFlowDialog;
    workFlowDialog.exec();
}

void MainFrame::on_toolButton_NetTest_clicked()
{
    EthernetTestDialog testDialog;
    testDialog.exec();
}
